<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="48">
          <a-col :md="6" :sm="24">
            <a-form-item label="社团名称">
              <a-input v-model="queryParam.name" placeholder="" />
            </a-form-item>
          </a-col>

          <a-col :xl="10" :lg="11" :md="12" :sm="24">
            <a-form-item label="成立日期">
              <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="queryParam.buildTime_begin"></j-date>
              <span class="query-group-split-cust"></span>
              <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="queryParam.buildTime_end"></j-date>
            </a-form-item>
          </a-col>
          <a-col :xl="4" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              
              <!-- <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'"/>
              </a> -->
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'Society:add'">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('社团')" v-has="'Society:export'">导出</a-button>
      
      <a-button type="primary" icon="import" @click="visibleImport=true" v-has="'Society:import'">导入</a-button>
      <XImport v-model="visibleImport" moduleName="orgSociety" @ok="searchQuery" @cancel="visibleImport=false"></XImport>
      <a-button type="primary" @click="handleCreate" style="margin-left: 8px">申请新社团</a-button>
              
      <a-dropdown v-if="selectedRowKeys.length > 0" v-has="'Society:batchdelete'">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel" v-has="'Society:batchdelete'"><a-icon type="delete"/>批量删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <s-table ref="table" size="default" :scroll="{x:true}" :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}" :columns="columns" :data="loadDataList" @onSelect="onChange">
      <span slot="action" slot-scope="text, record">
        <a @click="handleReview(record)">查看</a>
        <a @click="handleEdit(record)" v-has="record.isDirector?'Force':'Society:edit'" style="margin-left:10px">编辑</a>
        <a-popconfirm v-has="record.isDirector?'Force':'Society:delete'" style="margin-left:10px" title="确定删除?" @confirm="() => handleDelete(record.id)">
          <a>删除</a>
        </a-popconfirm>
        <a style="margin-left:10px" @click="handleJoin(record)" v-has="'Society:join'" :disabled="moment(record.dismissTime).isBefore(new Date())">申请加入</a>
      </span>
    </s-table>

    <!-- 社团弹窗 -->
    <SocietyModal ref="SocietyModal" @ok="searchQuery"></SocietyModal>

    <!-- 社团成员弹窗 -->
    <SocietyMemberModal ref="SocietyMemberModal" ></SocietyMemberModal>


  </a-card>
</template>

<script>
import STable from '@/components/table/'
import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
import ATextarea from 'ant-design-vue/es/input/TextArea'
import AInput from 'ant-design-vue/es/input/Input'

  // import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import moment from 'moment'
import {merge} from 'lodash'
import JDate from '@/components/jeecg/JDate.vue'
import XImport from '@/components/extends/XImport.vue'
import { getSocietyList ,delSociety,updateSociety,addSocietyMember} from '@/api/api'
import JoinSociety from './modules/JoinSociety'
import { getAction,postAction, deleteAction } from '@/api/manage'
import SocietyModal from './modules/SocietyModal'
import SocietyMemberModal from './modules/SocietyMemberModal'
export default {
  name: 'TableList',
  components: {
    JDate,
    AInput,
    ATextarea,
    STable,
    XImport,
    JoinSociety,
    SocietyModal,
    SocietyMemberModal,
    XSelectUserByDep
  },
  mixins:[JeecgListMixin],
  data() {
    return {
      moment,
      disableMixinCreated:true,
      visibleImport:false,
      toggleSearchStatus:false,
      form: null,
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {
        actStatus:10
      },
      url:{
        list:'/org/society/list',
        add:'/org/society/add',
        edit:'/org/society/edit',
        delete:'/org/society/delete',
        deleteBatch:'/org/society/deleteBatch',
        exportXlsUrl:'/org/society/exportXls',
      },
      // 表头
      columns: [
        {
          title: '社团名称',
          align: 'center',
          width:200,
          dataIndex: 'name'
        },
        {
          title: '所属部门',
          align: 'center',
          width:150,
          dataIndex: 'departName'
        },
        {
          title: '负责人',
          align: 'center',
          width:150,
          dataIndex: 'responsibleMan'
        },
        {
          title: '指导老师',
          align: "center",
          width: 100,
          dataIndex: 'instructor'
        },
        // {
        //   title: '学院',
        //   align: "center",
        //   width: 100,
        //   dataIndex: 'college'
        // },
        // {
        //   title: '专业',
        //   align: 'center',
        //   width:150,
        //   dataIndex: 'major'
        // },
        // {
        //   title: '年级',
        //   align: "center",
        //   width: 100,
        //   dataIndex: 'grade'
        // },
        // {
        //   title: '班级',
        //   align: "center",
        //   width: 100,
        //   dataIndex: 'clazz'
        // },
        {
          title: '成立时间',
          align: 'center',
          width:150,
          dataIndex: 'buildTime',
        },
        {
          title: '解散时间',
          align: 'center',
          width:150,
          dataIndex: 'dismissTime',
        },
        {
          title: '审批状态',
          align: 'center',
          width: 150,
          dataIndex: 'actStatus_dictText'
        },
        {
          title: '操作',
          align: 'center',
          // dataIndex: 'action',
          fixed:"right",
          width: 220,
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadDataList: parameter => {
        let params = Object.assign({},parameter, this.queryParam)
        params.name&&(params.name = `*${params.name}*`)
        return getAction(this.url.list,params).then(res => {
          if(res.success&&res.result){
            const obj = {
              data: res.result.records||[],
              pageNo: res.result.current,
              pageSize: res.result.size,
              totalCount: res.result.total,
              totalPage: res.result.page,
              sortColumns: res.result.sortColumns
            }
            return obj
          }else{
            return {}
          }
        })
      },

      selectedRowKeys: [],
      selectedRows: []
    }
  },
  methods: {
    handleAdd(){
      this.$refs.SocietyModal.title = '添加'
      this.$refs.SocietyModal.add();
    },
    handleCreate(){
      this.$refs.SocietyModal.title = '申请创建社团'
      this.$refs.SocietyModal.apply({
        responsibleManId: this.$store.getters.userInfo.id,
        responsibleMan: this.$store.getters.userInfo.username,
      });
    },
    handleEdit(record){
      this.$refs.SocietyModal.title = '编辑'
      this.$refs.SocietyModal.edit(record,{
        
      });
    },
    handleReview(record){
      this.$refs.SocietyModal.review(record);
    },
    handleToggleSearch(){
      this.toggleSearchStatus = !this.toggleSearchStatus;
    },
    // 申请加入
    handleJoin(record){
      this.$refs.SocietyMemberModal.title = '申请加入社团'
      this.$refs.SocietyMemberModal.apply({
        societyId:record.id,
        societyName:record.name,
        userId: this.$store.getters.userInfo.id,
        userName: this.$store.getters.userInfo.username,
      },{societyName:true});
    },
    handleDelete(id){
      delSociety({id}).then(res =>{
          if(res.success){
            this.$message.success('删除成功');
            this.searchQuery()
          }else{
            this.$message.error(res.message)
          }
      })
    },
    batchDel: function () {
      if(!this.url.deleteBatch){
        this.$message.error("请设置api.leteBatch属性!")
        return
      }
      if (this.selectedRowKeys.length <= 0) {
        this.$message.warning('请选择一条记录！');
        return;
      } else {
        var ids = "";
        for (var a = 0; a < this.selectedRowKeys.length; a++) {
          ids += this.selectedRowKeys[a] + ",";
        }
        var that = this;
        this.$confirm({
          title: "确认删除",
          content: "是否删除选中数据?",
          onOk: function () {
            that.loading = true;
            deleteAction(that.url.deleteBatch, {ids: ids}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.selectedRowKeys = [];
                that.selectionRows = [];
                that.searchQuery()
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.loading = false;
            });
          }
        });
      }
    },
    searchReset() {
      this.queryParam = {
        actStatus:10
      }
      this.searchQuery();
    },
    searchQuery() {
      this.$nextTick(() => {
        this.$refs.table.refresh()
      })
    },
    onSelectChange(selectedRowKeys, selectionRows) {
      this.selectedRowKeys = selectedRowKeys;
      this.selectionRows = selectionRows;
    },
    onChange(row) {
      this.selectedRowKeys = row.selectedRowKeys
      this.selectedRows = row.selectedRows
    },
    toggleAdvanced() {
      this.advanced = !this.advanced
    },
    resetSearchForm() {
      this.queryParam = {
        date: moment(new Date())
      }
    }
  }
}
</script>
